package cn.zust.edu.cn;

public class Triangle implements IShape {
    Point ptA;
    Point ptB;
    Point ptC;

    double edgeA;
    double edgeB;
    double edgeC;

    Triangle(Point ptA, Point ptB, Point ptC) {
        this.ptA = ptA;
        this.ptB = ptB;
        this.ptC = ptC;

        edgeA = distance(ptA, ptB);
        edgeB = distance(ptB, ptC);
        edgeC = distance(ptC, ptA);
    }

    @Override
    public double perimeter() {
        return edgeA + edgeB + edgeC;
    }

    @Override
    public double area() {
        double semiPerimeter = (edgeA + edgeB + edgeC) / 2;
        return Math.sqrt(semiPerimeter * (semiPerimeter - edgeA) * (semiPerimeter - edgeB) * (semiPerimeter - edgeC));
    }

    @Override
    public void draw() {
        System.out.println("Drawing Triangle");
        System.out.println("perimeter:"+perimeter());
        System.out.println("area:"+area());
    }

    private double distance(Point p1, Point p2) {
        return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));
    }
}